package com.kaluli.project.repository.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.kaluli.project.domain.entity.KllMainOrder;
import com.kaluli.project.domain.repository.KllMainOrderRepository;
import com.kaluli.system.repository.AbstractRepositorySupport;

@Repository
public class KllMainOrderRepositoryImpl extends AbstractRepositorySupport<KllMainOrder> implements KllMainOrderRepository{

	@Override
	protected Class<KllMainOrder> getEntityClass() {
		// TODO Auto-generated method stub
		return KllMainOrder.class;
	}

	@Override
	public List<KllMainOrder> getListLimited(int row) {
		
		StringBuffer hql=new StringBuffer(" from KllMainOrder t  where t.status=:status  ");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("status", 1);
		return this.findHqlForMap(hql.toString(), params);
	}

	@Override
	public List<KllMainOrder> getListByNSyn() {
		StringBuffer hql=new StringBuffer(" from KllMainOrder t  where t.status=1 and t.synApi=:synApi and t.financeAudit = :financeAudit  ");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("synApi", 0);
		params.put("financeAudit", 1);
		return this.findHqlForMap(hql.toString(), params);
	}
	
	

	@Override
	public boolean updateOrderSynApi(String orderNumber) {
		StringBuffer hql = new StringBuffer("update KllMainOrder t set t.synApi=1 where t.orderNumber=? ");
		//Map<String,Object> params=new HashMap<String, Object>();
		//params.put("orderNumber", orderNumber);
		String[] params = {orderNumber};
		int flag =  this.executeHql(hql.toString(), params);
		//boolean f = false;
		if(flag==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public KllMainOrder findByOrderNumber(String orderNumber) {
		StringBuffer hql=new StringBuffer(" from KllMainOrder t  where  t.orderNumber=:orderNumber  ");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("orderNumber", orderNumber);
		List<KllMainOrder> list = findHqlForMap(hql.toString(), params);
		KllMainOrder main = null;
		if(list!=null&&list.size()>0){
			main = list.get(0);
		}
		return main;
	}
	
	public KllMainOrder findByIbillingNumber(String ibillingNumber) {
		StringBuffer hql=new StringBuffer(" from KllMainOrder t  where  t.ibillingNumber=:ibillingNumber  ");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("ibillingNumber", ibillingNumber);
		List<KllMainOrder> list = findHqlForMap(hql.toString(), params);
		KllMainOrder main = null;
		if(list!=null&&list.size()>0){
			main = list.get(0);
		}
		return main;
	}
	
	
	

	public void updateApply(String reqBody, String orderNumber) {

		StringBuffer hql = new StringBuffer("update KllMainOrderAttr t set t.addressAttr=? where t.orderNumber=? ");
		Object[] param = { reqBody, orderNumber };
		this.executeHql(hql.toString(), param);
	}
	
	/**
	 * 更新子订单退款金额
	 * @param price
	 * @param fee
	 * @param orderNumber
	 */
	public void updateRefund(Double price,Double fee, String orderNumber) {

		StringBuffer hql = new StringBuffer("update KllMainOrderAttr t set t.refundPrice=?,t.refundExpressFee=? where t.orderNumber=? ");
		Object[] param = { price,fee, orderNumber };
		this.executeHql(hql.toString(), param);
	}
	
	/**
	 * 更新主订单退款金额
	 * @param price
	 * @param orderNumber
	 */
	public void updateMainRefund(Double price, String orderNumber) {

		StringBuffer hql = new StringBuffer("update KllMainOrder t set t.refund=?,status=4 where t.orderNumber=? ");
		Object[] param = { price,orderNumber };
		this.executeHql(hql.toString(), param);
	}
	
	/**
	 * 回写交易流水
	 * @param tradeNo
	 * @param orderNumber
	 */
	public void updateTradeNo(String tradeNo, String orderNumber) {

		StringBuffer hql = new StringBuffer("update KllMainOrder t set t.tradeNo=? where t.orderNumber=? ");
		Object[] param = { tradeNo,orderNumber };
		this.executeHql(hql.toString(), param);
	}
	
	/**
	 * 更新子订单状态
	 * @param orderNumber
	 */
	public void updateDetailRefund(String orderNumber) {
		//9表示取消
		StringBuffer hql = new StringBuffer("update KllMainOrderDetail t set t.status=9,updatedAt=? where t.orderNumber=? ");
		Object[] param = {new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), param);
	}
	
	/**
	 * 更新主订单状态
	 * @param price
	 * @param orderNumber
	 */
	public void updateMainStatus(String orderNumber) {

		StringBuffer hql = new StringBuffer("update KllMainOrder t set t.status=4 where t.orderNumber=? ");
		Object[] param = {orderNumber};
		this.executeHql(hql.toString(), param);
	}
	
	
	

}
